package org.apache.osgimaker.wizard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.osgimaker.Messages;
import org.apache.osgimaker.analyse.AnalyseJob;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

/**
 * @author Tiger Gui
 * tigergui@gmail.com - Beijing China
 * 
 */
public class SourceSelectionDialog extends Dialog {

	private JavaProject project;
	private Combo combo;
	private HashMap<String, Boolean> map = new HashMap<String, Boolean>();
	Button button_pack;
	Button button_single;
	
	public SourceSelectionDialog(Shell parentShell, JavaProject project) {
		super(parentShell);
		super.setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN);
		this.project = project;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);

		Composite c2 = new Composite(composite, SWT.NONE);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		gridLayout.horizontalSpacing = 0;
		gridLayout.verticalSpacing = 5;
		gridLayout.marginWidth = 0;
		gridLayout.marginHeight = 10;
		c2.setLayout(gridLayout);
		GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true);
		gd.widthHint = 600;
		gd.heightHint = 200;
		
		Table table = new Table(c2, SWT.CHECK | SWT.BORDER | SWT.FILL | SWT.FILL);
		table.setLayoutData(gd);
		//table.setHeaderVisible(true);
        //table.setLinesVisible(true);
		
		String[] items = getProjectSourceList();
		for(int i=0; i<items.length; i++){
			TableItem item = new TableItem(table, SWT.NONE);
		    item.setText(items[i]);
		    item.setChecked(true);
		    map.put(item.getText(), true);
		}
		table.addSelectionListener(new SelectionListener(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem item = (TableItem)e.item;
				if(item.getChecked()) map.put(item.getText(), true);
				else map.put(item.getText(), false);
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			
		});
		
		final Group group = new Group(c2, SWT.NONE);
		group.setText("Select Default bundle split section");
		group.setBounds(22, 10, 600, 100);

	    button_pack = new Button(group, SWT.RADIO);
	    button_pack.setText("Package Only");
	    button_pack.setBounds(10, 26, 93, 16);
	    button_pack.setSelection(true);
		  
		button_single=new Button(group,SWT.RADIO);
		button_single.setText("All Class File");
		button_single.setBounds(110, 26, 93, 16);
		
		GridData groupData = new GridData(GridData.FILL, GridData.FILL, true, true);
		gd.widthHint = 600;
		gd.heightHint = 100;
		group.setLayoutData(groupData);

		return composite;
	}
	private String[] getProjectSourceList(){
		List<String> srcList = new ArrayList<String>();
		try {
			IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
			for (int i = 0; i < roots.length; i++) {
				IPackageFragmentRoot root = roots[i];{
					if(!root.isArchive()) srcList.add(root.getElementName());
				}
			}
			return srcList.toArray(new String[0]);
		} catch (JavaModelException e) {
			e.printStackTrace();
		}
		return null;
	}


	@Override
	protected void okPressed() {
		List<String> srcFolder = new ArrayList<String>();
		Iterator<String> ite = map.keySet().iterator();
		while(ite.hasNext()){
			String key = ite.next();
			if(map.get(key)) srcFolder.add(key);
		}
		String basicSection = Messages.PackageBasicSection;
		if(this.button_single.getSelection()) basicSection = Messages.ClassBasicSection;
		AnalyseJob job = new AnalyseJob("AnalyseJob", project, srcFolder, basicSection);
		job.schedule();
		
		super.okPressed();
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Please select source file folders to analyse");
	}
}

